package com.ship.dispatch.controller;

import com.github.pagehelper.PageInfo;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.FileUtils;
import com.ship.common.core.validate.Add;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.dispatch.bean.ConResourceMemorandum;
import com.ship.dispatch.bean.IshShipBaseExtend;
import com.ship.dispatch.bean.SpInformation;
import com.ship.dispatch.bean.boats.info.QueryShipMessageInfo;
import com.ship.dispatch.bean.treaShipNetwork.IhsShipBaseDetailVO;
import com.ship.dispatch.bean.treaShipNetwork.IhsShipBaseList;
import com.ship.dispatch.bean.treaShipNetwork.IhsShipBaseSelVo;
import com.ship.dispatch.bo.*;
import com.ship.dispatch.model.ShipQueryModel;
import com.ship.dispatch.service.SpInformationService;
import com.ship.dispatch.service.SpRouteService;
import com.ship.dispatch.util.UploadFreightUtil;
import com.ship.dispatch.vo.PortVo;
import com.ship.dispatch.vo.SpOverhaulVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.beans.IntrospectionException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 船舶信息 前端控制器
 *
 * @author AbyssRabbit@163.com
 * @since 2023-10-18
 */
@Api(value = "spInformation", tags = "船舶信息")
@RestController
@RequestMapping("/spInformation/")
public class SpInformationController {

    @Autowired
    private SpInformationService spInformationService;

    @Autowired
    private UploadFreightUtil uploadFreightUtil;

    @Autowired
    private SpRouteService spRouteService;

    @ApiOperation("手动新增虚拟船只")
    @PostMapping("virtual/add")
    public JsonResult virtualAdd(@RequestBody @Validated(Add.class) VirtualShipAddBo bo) {
        return spInformationService.virtualAdd(bo);
    }

    @ApiOperation("手动编辑虚拟船只")
    @PostMapping("virtual/update")
    public JsonResult virtualUpdate(@RequestBody @Validated VirtualShipAddBo bo) {
        return spInformationService.virtualUpdate(bo);
    }

    @ApiOperation("船只虚拟租入或租出")
    @PostMapping("virtual/rent")
    public JsonResult virtualRent(@RequestBody ShipRentUpdateBo bo) {
        return spInformationService.virtualRent(bo);
    }

    @ApiOperation("批量上传船舶信息列表")
    @PostMapping("upload/shipMessage")
    public JsonResult uploadShipMessage(@RequestParam("file") MultipartFile file) throws Exception {
        FileUtils.checkFileName(file);
        Map<String, List<IshShipBaseExtend>> map = uploadFreightUtil.uploadList(file);
        boolean status = spInformationService.createShipBaseExtend(map);
        return JsonResult.success(status);
    }

    @ApiOperation("根据imo号查询船舶信息内部档案")
    @GetMapping("query/shipMessageInterior/byImo")
    public JsonResult queryShipMessageInteriorByImo(@RequestParam String imo){
        IshShipBaseExtend info = spInformationService.queryShipMessageInteriorByImo(imo);
        return JsonResult.success(info);
    }

    @ApiOperation("分页查询在建船舶信息列表")
    @GetMapping("query/underConstruction/page")
    public JsonResult queryUnderConstructionPage(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        PageInfo<IshShipBaseExtend> pageInfo =  spInformationService.queryUnderConstructionPage(pageNum,pageSize);
        return JsonResult.success(pageInfo);
    }

    /**
     * 获取船旗国
     *
     * @return
     */
    @GetMapping("getFlagStates")
    public JsonResult<List<String>> getFlagStates() {
        List<String> flagStates = spInformationService.getFlagStates();
        return JsonResult.success(flagStates);
    }

    /**
     * 根据条件获取船舶
     *
     * @param shipQueryModel
     */
    @PostMapping("getShips")
    public JsonResult<List<SpInformation>> getShips(@RequestBody ShipQueryModel shipQueryModel) {
        List<SpInformation> ships = spInformationService.getShips(shipQueryModel);
        return JsonResult.success(ships);
    }

    /**
     * 获取港口信息
     *
     * @return
     */
    @GetMapping("getPort")
    public JsonResult<List<PortVo>> getPort() {
        List<PortVo> res = spInformationService.getPort();
        return JsonResult.success(res);
    }

    /**
     * 获取港口信息树形结构
     *
     * @return
     */
    @GetMapping("getPort/tree")
    public JsonResult<List<PortTreeSelect>> getPortTree() {
        return JsonResult.success(spInformationService.getPortTree());
    }

    /**
     * 获取资源合同
     *
     * @return
     */
    @GetMapping("getCon")
    public JsonResult<List<ConResourceMemorandum>> getCon() {
        List<ConResourceMemorandum> res = spInformationService.getCon();
        return JsonResult.success(res);
    }

    /**
     * 添加虚拟船(调度优化)
     *
     * @param virtualShipBO
     */
    @PostMapping("saveOptimizeVirtualShip")
    @Log(title = "添加虚拟船(调度优化)",businessType = BusinessType.INSERT)
    public JsonResult saveOptimizeVirtualShip(@RequestBody @Validated VirtualShipOptimizeBO virtualShipBO) {
        spInformationService.saveOptimizeVirtualShip(virtualShipBO);
        return JsonResult.success();
    }

    /**
     * 分页查询
     * @param ihsShipBaseSelVo
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public JsonResult<IhsShipBaseList> getSchemeDetailPage(@RequestBody @Validated IhsShipBaseSelVo ihsShipBaseSelVo) {
        JsonResult<IhsShipBaseList> page = spInformationService.getListInfo(ihsShipBaseSelVo);
        return page;
    }

    /**
     * 获取查询选项
     * @return
     */
    @ApiOperation(value = "获取查询选项")
    @GetMapping("/queryCriteria")
    public JsonResult queryCriteria() {
        return JsonResult.success(spInformationService.getQueryCriteria());
    }

    /**
     * 查看船舶详情
     * @param imo
     * @return
     * @throws IntrospectionException
     */
    @ApiOperation(value = "查看船舶详情")
    @GetMapping("/getShipInfoDetail/{imo}")
    public JsonResult getShipInfoDetail(@PathVariable(value = "imo") String imo) throws IntrospectionException {
        List<IhsShipBaseDetailVO> shipReportDetailVOS = spInformationService.getShipInfoDetail(imo);
        return JsonResult.success(shipReportDetailVOS);
    }

    @ApiOperation(value = "查看船舶详情，获取时间")
    @GetMapping("/getShipInfoDetail/time/{imo}")
    public JsonResult getShipInfoDetailTime(@PathVariable(value = "imo") String imo){
        String time = spInformationService.getShipInfoDetailTime(imo);
        return JsonResult.success(time);
    }

    /**
     * 导出
     * @param spOverhaulVo
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public JsonResult<Object> export(@RequestBody SpOverhaulVo spOverhaulVo) throws IOException {
        String url = spInformationService.export(spOverhaulVo);
        return JsonResult.success(url);
    }

    /**
     * 上传 fromB
     */
    @ApiOperation(value = "上传 fromB")
    @PostMapping("/uploadFromB")
    @Log(title = "上传文件",businessType = BusinessType.IMPORT)
    public JsonResult upload(@RequestParam("file") MultipartFile file,String imo) {
        FileUtils.checkFileName(file);
            return JsonResult.success(spInformationService.upload(file,imo));
    }

    @ApiOperation("根据fromB附件id删除附件信息")
    @PostMapping("/delete/fromB/byId")
    public JsonResult deleteFromBById(@RequestBody List<String> ids){
        boolean status = spInformationService.deleteFromBById(ids);
        return JsonResult.success(status);
    }



    /**
     * 获取FromB信息
     * @return
     */
    @ApiOperation(value = "获取FromB连接")
    @GetMapping("/getFromB/{imo}")
    public JsonResult getFromB(@PathVariable(value = "imo") String imo){
        List<Map<String,Object>> result = spInformationService.getFromB(imo);
        return JsonResult.success(result);
    }

    @ApiOperation("查询所有船舶信息-海图")
    @GetMapping("/query/shipMessage/all")
    public JsonResult queryShipMessageAll(){
        List<QueryShipMessageInfo> shipMessageList = spInformationService.queryShipMessageAll();
        return JsonResult.success(shipMessageList);
    }
}
